#include "ueq_eq.h"

#include <string.h>
#include "ueq_fifo.h"

void ueq_event_queue_init(UeqEventQueue *peq, BYTE *pb, INT32 size)
{
    UEQ_ASSERT(sizeof(BYTE) == sizeof(UeqEvent));
    UEQ_ASSERT(peq != NULL);
    UEQ_ASSERT(pb != NULL);
    ueq_fifo_init(&peq->cb, pb, size);
    memset(peq->event_counts, 0, sizeof(peq->event_counts));
}

void ueq_event_queue_reset(UeqEventQueue *peq)
{
    UEQ_ASSERT(sizeof(BYTE) == sizeof(UeqEvent));
    UEQ_ASSERT(peq != NULL);
    ueq_fifo_reset(&peq->cb);
    memset(peq->event_counts, 0, sizeof(peq->event_counts));
}

BOOL ueq_event_queue_is_full(UeqEventQueue *peq)
{
    UEQ_ASSERT(sizeof(BYTE) == sizeof(UeqEvent));
    UEQ_ASSERT(peq != NULL);
    return ueq_fifo_is_full(&peq->cb);
}

BOOL ueq_event_queue_is_empty(UeqEventQueue *peq)
{
    UEQ_ASSERT(sizeof(BYTE) == sizeof(UeqEvent));
    UEQ_ASSERT(peq != NULL);
    return ueq_fifo_is_empty(&peq->cb);
}

BOOL ueq_event_queue_contains(UeqEventQueue *peq, UeqEvent event)
{
    UEQ_ASSERT(sizeof(BYTE) == sizeof(UeqEvent));
    UEQ_ASSERT(peq != NULL);
    UEQ_ASSERT(event < UEQ_NUM_EVENTS);
    return (BOOL)(peq->event_counts[event] > 0);
}

/* User needs to check if the event queue is full */
/* It is not allowed to write to a full queue !!! */
void ueq_event_queue_push_back(UeqEventQueue *peq, UeqEvent event)
{
    UEQ_ASSERT(sizeof(BYTE) == sizeof(UeqEvent));
    UEQ_ASSERT(peq != NULL);
    UEQ_ASSERT(event < UEQ_NUM_EVENTS);
    ueq_fifo_push_back(&peq->cb, event);
    ++peq->event_counts[event];
}

/* User needs to check if the event queue is empty first */
UeqEvent ueq_event_queue_pop_front(UeqEventQueue *peq)
{
    UeqEvent event = UEQ_EVENT_NULL;
    UEQ_ASSERT(sizeof(BYTE) == sizeof(UeqEvent));
    UEQ_ASSERT(peq != NULL);
    event = ueq_fifo_pop_front(&peq->cb);
    UEQ_ASSERT(event < UEQ_NUM_EVENTS);
    --peq->event_counts[event];
    return event;
}
